package com.baijiayun.liveuibase.loading;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import androidx.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ProgressBar;

import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.DisplayUtils;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;

/**
 * Created by Shubo on 2018/10/17.
 */
public class ProgressBarWithNumber extends ProgressBar {

    /**
     * 百分比字符大小
     */
    private static final int TEXT_SIZE_IN_SP = 14;

    /**
     * 默认Padding Top，画百分比字符可能超出View边界，所以需要加上padding
     */
    private static final int DEFAULT_PADDING_TOP_IN_DP = 20;

    /**
     * 默认Padding Left/Right，画百分比字符可能超出View边界，所以需要加上padding
     */
    private static final int DEFAULT_PADDING_IN_DP = 20;

    /**
     * 小三角的高
     */
    private static final int DEFAULT_TRIANGLE_HEIGHT = 3;

    /**
     * 小三角的1/2宽
     */
    private static final int DEFAULT_TRIANGLE_HALF_WIDTH = 3;

    protected Paint mTextPaint, trianglePaint;
    private int padding;
    private int triangleH;
    private int triangleHalfW;
    private Path trianglePath;

    public ProgressBarWithNumber(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressBarWithNumber(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ProgressBarWithNumber(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mTextPaint = new Paint();
        mTextPaint.setColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(getContext(), R.attr.base_theme_live_product_color));
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(DisplayUtils.sp2px(getContext(), TEXT_SIZE_IN_SP));
        padding = DisplayUtils.dip2px(getContext(), DEFAULT_PADDING_IN_DP);
        setPadding(padding, DisplayUtils.dip2px(getContext(), DEFAULT_PADDING_TOP_IN_DP), padding, 0);

        trianglePaint = new Paint();
        trianglePaint.setColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(getContext(), R.attr.base_theme_live_product_color));
        trianglePaint.setAntiAlias(true);
        trianglePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        triangleH = DisplayUtils.dip2px(getContext(), DEFAULT_TRIANGLE_HEIGHT);
        triangleHalfW = DisplayUtils.dip2px(getContext(), DEFAULT_TRIANGLE_HALF_WIDTH);
        trianglePath = new Path();
        setLayerType(View.LAYER_TYPE_SOFTWARE,mTextPaint);
        setLayerType(View.LAYER_TYPE_SOFTWARE,trianglePaint);
        LayerDrawable layerDrawable = (LayerDrawable) getProgressDrawable();
        Drawable progressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.progress);
        Drawable secondProgressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.secondaryProgress);
        final int productColor = ThemeDataUtil.getColorFromThemeConfigByAttrId(getContext(), R.attr.base_theme_live_product_color);
        progressDrawable.setColorFilter(productColor, PorterDuff.Mode.SRC_IN);
        secondProgressDrawable.setColorFilter(productColor, PorterDuff.Mode.SRC_IN);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float percent = getProgress() * 1.f / getMax();
        String text = getProgress() + "%";
        float textWidth = mTextPaint.measureText(text);
        float textHeight = mTextPaint.descent() + mTextPaint.ascent();
        float progressedLength = (getWidth() - 2 * padding) * percent;
        float x = progressedLength + padding - 0.5f * textWidth;
        canvas.drawText(text, x, -textHeight + 2, mTextPaint);

        float trianglePadding = getHeight() - 2 * triangleH;

        trianglePath.reset();
        trianglePath.moveTo(progressedLength + padding, trianglePadding);
        trianglePath.lineTo(progressedLength + padding - triangleHalfW, trianglePadding - triangleH);
        trianglePath.lineTo(progressedLength + padding + triangleHalfW, trianglePadding - triangleH);
        trianglePath.lineTo(progressedLength + padding, trianglePadding);
        trianglePath.close();

        canvas.drawPath(trianglePath, trianglePaint);
    }

    public void setDescColor(String color) {
        if (TextUtils.isEmpty(color)) {
            mTextPaint.setColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(getContext(), R.attr.base_theme_live_product_color));
            trianglePaint.setColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(getContext(), R.attr.base_theme_live_product_color));
        } else {
            try {
                mTextPaint.setColor(Color.parseColor(color));
                trianglePaint.setColor(Color.parseColor(color));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setProgressBarColor(String color) {
        if (!TextUtils.isEmpty(color)) {
            LayerDrawable layerDrawable = (LayerDrawable) getProgressDrawable();
            Drawable progressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.progress);
            Drawable secondProgressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.secondaryProgress);
            progressDrawable.setColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_IN);
            secondProgressDrawable.setColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_IN);
        }
    }
}
